﻿using System;
using System.Linq.Expressions;
using System.Reflection;
using MoroMoro.Samples.ToDoList.Base.Domain.Expanders;
using MoroMoro.Samples.ToDoList.Base.Domain.Viewpoints;

namespace MoroMoro.Samples.ToDoList.Domain.ToDos
{
    /// <summary>
    /// <see cref="ToDo"/>の<see cref="ToDo.State">状態</see>を分析します。
    /// </summary>
    [Serializable]
    public sealed class ToDoStateViewpoint : Viewpoint<ToDo, ToDoState>, IExpandable<ToDo>
    {
        /// <inheritdoc/>
        protected override Expression<Func<ToDo, ToDoState>> CreateExpression()
        {
            return entity =>
                entity.CompletedDateTime.HasValue ? ToDoState.Completed :
                entity.TouchedDateTime.HasValue   ? ToDoState.Touched :
                                                    ToDoState.Untouched;
        }


        /// <inheritdoc/>
        PropertyInfo IExpandable<ToDo>.From()
        {
            return ReflectionHelper.ExtractPropertyInfo((ToDo entity) => entity.State);
        }

        /// <inheritdoc/>
        Expression IExpandable<ToDo>.To()
        {
            return Expression.Body;
        }
    }
}
